import './assets/main.css'

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import axios from 'axios'

// 配置后端接口基础地址（与后端端口一致，使用你的8081端口）
axios.defaults.baseURL = 'http://localhost:8081'

// 请求拦截器：携带token
axios.interceptors.request.use(config => {
    const token = localStorage.getItem('token')
    if (token) {
        config.headers.token = token
    }
    return config
})

// 响应拦截器：处理错误
axios.interceptors.response.use(
    response => response,
    error => {
        if (error.response?.status === 401) {
            // 未登录或token失效，跳转到登录页
            localStorage.removeItem('token')
            router.push('/')
            alert('登录已过期，请重新登录')
        }
        return Promise.reject(error)
    }
)

const app = createApp(App)
app.use(router)
app.use(ElementPlus)
app.config.globalProperties.$axios = axios // 全局挂载axios
app.mount('#app')